package no.ntnu.fp.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.Timer;

import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Component;
import javax.swing.Box;

import run.Main;

import java.awt.Dimension;
import java.beans.PropertyVetoException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import javax.swing.SwingConstants;
import com.michaelbaranov.microba.calendar.DatePicker;
import java.awt.Color;
import javax.swing.JSeparator;

import no.ntnu.fp.net.DBConnection;
import javax.swing.ScrollPaneConstants;
import javax.swing.JComboBox;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private static Box verticalBoxDay1, verticalBoxDay2, verticalBoxDay3, verticalBoxBottom;
	private DatePicker datePicker;
	private Calendar calendar = Calendar.getInstance();
	private SimpleDateFormat dateFormat = new SimpleDateFormat("EEE - dd.MMM, yyyy");
	private static SimpleDateFormat dateFormatDay = new SimpleDateFormat("dd.MM.yy");
	private JLabel labelDay1,labelDay2,labelDay3;
	private static Image imageMeeting = Toolkit.getDefaultToolkit().createImage(
			MainFrame.class.getResource("/images/group.png"));
	private static Image imageAppointment = Toolkit.getDefaultToolkit().createImage(
			MainFrame.class.getResource("/images/single.png"));
	private static Image imageAttending = Toolkit.getDefaultToolkit().createImage(
			MainFrame.class.getResource("/images/grouplocked.png"));
	private JComboBox day2comboBox;
	private JComboBox day3comboBox;
	private JButton btnMulti1;
	private HashMap<String, Integer> reverseEmployeeMap;
	private static int currentUserId = Main.user.getEmployeeId();
	private static Timer updateTimer; //Timer for timer-functions
	
	/**
	 * Create the frame.
	 */
	public MainFrame() {
		
		Main.dbConn = new DBConnection();
		
		/* 
		 * Timer function
		 * 
		 * */
		long delay = 5 * 1000;
		updateTimer = new javax.swing.Timer(
				(int)delay, new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						getMeetingRequest(verticalBoxBottom); //updates meetingRequests Box
						if (btnMulti1.getText().equals("Compare")){
							updateSingleBoxes();
							}
						else {
							updateMultiBoxes();
							}
						refreshCalendarUI();
					}
				});
		
		setBackground(Color.WHITE);
		setTitle("Calendar System - Main (User: "+ Main.user.getUsername()+")");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1014, 708);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panelDay1 = new JPanel();
		panelDay1.setBackground(Color.WHITE);
		panelDay1.setBounds(262, 78, 241, 477);
		contentPane.add(panelDay1);
		panelDay1.setLayout(null);
		
		labelDay1 = new JLabel("");
		labelDay1.setBounds(66, 0, 128, 16);
		panelDay1.add(labelDay1);
		labelDay1.setFont(new Font("Tahoma", Font.BOLD, 13));
		labelDay1.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		JScrollPane scrollPaneDay1 = new JScrollPane();
		scrollPaneDay1.setBounds(0, 18, 241, 458);
		panelDay1.add(scrollPaneDay1);
		verticalBoxDay1 = Box.createVerticalBox();
		verticalBoxDay1.setBackground(Color.WHITE);
		scrollPaneDay1.setViewportView(verticalBoxDay1);
		
		JPanel panelDay2 = new JPanel();
		panelDay2.setBackground(Color.WHITE);
		panelDay2.setLayout(null);
		panelDay2.setBounds(503, 78, 239, 477);
		contentPane.add(panelDay2);
		
		labelDay2 = new JLabel("");
		labelDay2.setBounds(66, 0, 127, 16);
		panelDay2.add(labelDay2);
		labelDay2.setFont(new Font("Tahoma", Font.BOLD, 13));
		labelDay2.setAlignmentX(0.5f);
		
		JScrollPane scrollPaneDay2 = new JScrollPane();
		scrollPaneDay2.setBounds(2, 18, 237, 458);
		panelDay2.add(scrollPaneDay2);
		
		verticalBoxDay2 = Box.createVerticalBox();
		scrollPaneDay2.setViewportView(verticalBoxDay2);
		
		JPanel panelDay3 = new JPanel();
		panelDay3.setBackground(Color.WHITE);
		panelDay3.setLayout(null);
		panelDay3.setBounds(744, 78, 241, 477);
		contentPane.add(panelDay3);
		
		labelDay3 = new JLabel("");
		labelDay3.setBounds(69, 0, 126, 16);
		panelDay3.add(labelDay3);
		labelDay3.setFont(new Font("Tahoma", Font.BOLD, 13));
		labelDay3.setAlignmentX(0.5f);
		
		JScrollPane scrollPaneDay3 = new JScrollPane();
		scrollPaneDay3.setBounds(0, 18, 241, 458);
		panelDay3.add(scrollPaneDay3);
		
		verticalBoxDay3 = Box.createVerticalBox();
		scrollPaneDay3.setViewportView(verticalBoxDay3);
		
		JPanel panelTop = new JPanel();
		panelTop.setBackground(Color.WHITE);
		panelTop.setBounds(10, 11, 975, 66);
		contentPane.add(panelTop);
		panelTop.setLayout(null);
		
		JLabel lblDynamicLabel = new JLabel("user: "+ Main.user.getUsername());
		lblDynamicLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDynamicLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDynamicLabel.setBounds(821, 0, 139, 22);
		panelTop.add(lblDynamicLabel);
		
		datePicker = new DatePicker();
		datePicker.setFieldEditable(false);
		datePicker.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateTimer.stop(); //stop timer
				if (btnMulti1.getText().equals("Compare")){
					updateSingleBoxes();
					}
				else {
					updateMultiBoxes();
					}
				refreshCalendarUI();
				updateTimer.start(); //start timer
			}
		});
		datePicker.setBounds(252, 44, 118, 22);
		panelTop.add(datePicker);
		datePicker.getDate();
		
		JLabel lblCalendarSystem = new JLabel("Calendar System");
		lblCalendarSystem.setBounds(20, 11, 194, 42);
		panelTop.add(lblCalendarSystem);
		lblCalendarSystem.setFont(new Font("Tahoma", Font.PLAIN, 26));
		
		JSeparator separator = new JSeparator();
		separator.setBounds(20, 64, 206, 2);
		panelTop.add(separator);
		
		JLabel lblV = new JLabel("v0.1");
		lblV.setBounds(191, 45, 35, 14);
		panelTop.add(lblV);
		
		JButton btnBack = new JButton("<==");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				calendar.setTime(datePicker.getDate());
				calendar.add(Calendar.DATE,-1); //decrease by one day
				try {
					datePicker.setDate(calendar.getTime());} 
				catch (PropertyVetoException e) {
					e.printStackTrace();
					}
			}
		});
		btnBack.setBounds(371, 44, 60, 22);
		panelTop.add(btnBack);
		
		JButton btnForward = new JButton("==>");
		btnForward.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				calendar.setTime(datePicker.getDate());
				calendar.add(Calendar.DATE, 1); //increment by one day
				try {
					datePicker.setDate(calendar.getTime());} 
				catch (PropertyVetoException e) {
					e.printStackTrace();
					}
			}
		});
		btnForward.setBounds(433, 44, 60, 22);
		panelTop.add(btnForward);
		
		day2comboBox = new JComboBox();
		day2comboBox.setBounds(559, 44, 130, 20);
		panelTop.add(day2comboBox);
		
		day3comboBox = new JComboBox();
		day3comboBox.setBounds(802, 46, 130, 20);
		panelTop.add(day3comboBox);
		
		btnMulti1 = new JButton("Compare");
		btnMulti1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(btnMulti1.getText().equals("Compare")){
					updateMultiBoxes();
					btnMulti1.setText("Cancel");
				}
				else{
					updateSingleBoxes();
					btnMulti1.setText("Compare");
				}
				refreshCalendarUI();
			}
			
		});
		btnMulti1.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnMulti1.setBounds(699, 43, 93, 23);
		panelTop.add(btnMulti1);
		
		JPanel panelLeft = new JPanel();
		panelLeft.setBackground(Color.WHITE);
		panelLeft.setBounds(10, 78, 252, 477);
		contentPane.add(panelLeft);
		panelLeft.setLayout(null);
		
		JButton btnManageAppointments = new JButton("Manage Appointments");
		btnManageAppointments.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnManageAppointments.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showManageAppointment();
			}
		});
		btnManageAppointments.setBounds(22, 25, 207, 41);
		panelLeft.add(btnManageAppointments);
		
		JButton btnTrackMeetingRequest = new JButton("Track meeting requests");
		btnTrackMeetingRequest.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnTrackMeetingRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showTrackMeetingRequest();
			}
		});
		btnTrackMeetingRequest.setBounds(22, 72, 207, 41);
		panelLeft.add(btnTrackMeetingRequest);
		
		JButton btnAddMeetingRoom = new JButton("Add meeting room");
		btnAddMeetingRoom.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnAddMeetingRoom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showAddMeetingRoom();
			}
		});
		btnAddMeetingRoom.setBounds(22, 120, 207, 41);
		panelLeft.add(btnAddMeetingRoom);
		
		JLabel lblNewLabel = new JLabel("Meeting Requests:");
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
		lblNewLabel.setBounds(22, 452, 207, 19);
		panelLeft.add(lblNewLabel);
		
		JPanel panelBottom = new JPanel();
		panelBottom.setBounds(10, 554, 975, 107);
		contentPane.add(panelBottom);
		panelBottom.setLayout(null);
		
		JScrollPane scrollPaneBottom = new JScrollPane();
		scrollPaneBottom.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPaneBottom.setBounds(20, 0, 955, 107);
		panelBottom.add(scrollPaneBottom);
		
		verticalBoxBottom = Box.createVerticalBox();
		scrollPaneBottom.setViewportView(verticalBoxBottom);
		updateSingleBoxes();
		getMeetingRequest(verticalBoxBottom);
		populateComboBox();
		updateTimer.start();

	}
	
	
	/**
	 * Runs query and populates calendarBox with data from given calendarDate
	 * @param calendarDate Date
	 * @param calendarBox Box
	 * @throws ParseException
	 */
	public static void getNewCalendar(Date calendarDate, Box calendarBox, int userId, boolean isOwner) {
		calendarBox.removeAll();
		long lowUnixTime = 0, highUnixTime = 0;
		try {
			lowUnixTime = ManageAppointment.generateLowUnixStamp(
					ManageAppointment.generateReadableUnixTime(calendarDate));
			highUnixTime = ManageAppointment.generateHighUnixStamp(
					ManageAppointment.generateReadableUnixTime(calendarDate));
		} 
		catch (ParseException e1) {
			e1.printStackTrace();
		}	
		
		try {			
			ResultSet appointmentRes = Main.dbConn.selectFromDatabase(
					"(SELECT appointment.* FROM appointment WHERE appointment.calendarId ='"+
					userId +"' AND appointment.date BETWEEN "
					+lowUnixTime+" AND "+ highUnixTime + ")"+
					" UNION (SELECT appointment.* FROM appointment,meetingRequest"+
					" WHERE meetingRequest.recipientId = " + userId +
					" AND meetingRequest.status ='ACCEPTED' AND meetingRequest.employeeId =" +
					" appointment.calendarId AND meetingRequest.meetingId = appointment.appointmentId" +
					" AND appointment.date BETWEEN "
					+lowUnixTime+" AND "+ highUnixTime +") ORDER BY starttime ASC ");
			while(appointmentRes.next()) {
				int meetingLeaderId = Integer.parseInt(appointmentRes.getString("calendarId"));
				if (meetingLeaderId != userId){
					createNewDayBox(  //If appointment is a attending appointment
							calendarBox, 
							formatTime(appointmentRes.getString("starttime")) + " - " + 
							formatTime(appointmentRes.getString("finishtime")),
							appointmentRes.getString("description"),
							"Meeting leader: " + getMeetingLeader(meetingLeaderId),
							imageAttending,
							isOwner,
							appointmentRes.getInt("appointmentId")
							);
				}
				else if(ManageAppointment.isAppointmentMeeting(appointmentRes.getInt("appointmentId"), userId)){
					createNewDayBox(  //If appointment is a meeting
							calendarBox, 
							formatTime(appointmentRes.getString("starttime")) + " - " + 
							formatTime(appointmentRes.getString("finishtime")),
							appointmentRes.getString("description"),
							"",
							imageMeeting,
							false,
							(0)
							);
				}
				else { //If appointment
					createNewDayBox(
							calendarBox, 
							formatTime(appointmentRes.getString("starttime")) + " - " + 
							formatTime(appointmentRes.getString("finishtime")),
							appointmentRes.getString("description"),
							"",
							imageAppointment,
							false,
							(0)
						);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Creates UI-components and fills it with data from parameter,
	 * and inserts it into given calendarBox
	 * @param calendarBox
	 * @param time
	 * @param description
	 * @param notes
	 * @param image
	 */
	private static void createNewDayBox(Box calendarBox, String time,
			String description, String notes, Image image, boolean isOwner, int meetingId){
		Box horizontalBox_1 = Box.createHorizontalBox();
		horizontalBox_1.setBorder(new LineBorder(Color.LIGHT_GRAY, 1, true));
		calendarBox.add(horizontalBox_1);
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(image));
		horizontalBox_1.add(label);
		
		Component rigidArea_1 = Box.createRigidArea(new Dimension(5, 20));
		horizontalBox_1.add(rigidArea_1);
		Box verticalBox2_1 = Box.createVerticalBox();
		horizontalBox_1.add(verticalBox2_1);
		JLabel label_1 = new JLabel(time);
		label_1.setFont(new Font("Tahoma", Font.BOLD, 17));
		verticalBox2_1.add(label_1);
		JLabel label_2 = new JLabel(description);
		label_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		verticalBox2_1.add(label_2);
		JLabel label_3 = new JLabel(notes);
		label_3.setForeground(Color.RED);
		label_3.setFont(new Font("Tahoma", Font.PLAIN, 12));
		verticalBox2_1.add(label_3);
		
		Component horizontalGlue = Box.createHorizontalGlue();
		horizontalBox_1.add(horizontalGlue);
		
		if (isOwner){
			final int meetingIdFinal = meetingId;
			final JLabel label_4 = new JLabel(" ");			
			label_4.setIcon(new ImageIcon(MainFrame.class.getResource("/images/deny.png")));
			label_4.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent arg0) {
					setMeetingStatus(meetingIdFinal,"DECLINED");
					label_4.setEnabled(false);
				}
			});		
			horizontalBox_1.add(label_4);
		}
		
		
		refreshCalendarUI();
	}
	
	
	
	/**
	 * Runs query and populates meetingBox with data from current userId
	 */
	public static void getMeetingRequest(Box meetingBox) {
		 meetingBox.removeAll();
		ResultSet meetingRequestRes;
		try {
			meetingRequestRes = Main.dbConn.selectFromDatabase(
					"SELECT * FROM appointment,meetingRequest,meetingRoom WHERE meetingRequest.recipientId =" +
					currentUserId + " AND meetingRequest.status ='UNKNOWN' " +
					"AND meetingRequest.meetingId = appointment.appointmentId " +
					"AND meetingRoom.meetingRoomId = meetingRequest.meetingRoomId ORDER by date ASC"
			);
			while(meetingRequestRes.next()) {
				long unixTime = Long.parseLong(meetingRequestRes.getString("date"));
				createNewMeetingRequestBox(meetingBox, dateFormatDay.format(new java.util.Date(unixTime*1000))+ " ("+
						formatTime(meetingRequestRes.getString("starttime")) + " - " + 
						formatTime(meetingRequestRes.getString("finishtime")) + ")",
						meetingRequestRes.getString("description"),
						getMeetingLeader(meetingRequestRes.getInt("employeeId")),
						meetingRequestRes.getString("roomName")+" - ("+meetingRequestRes.getString("location")+")",
						meetingRequestRes.getInt("meetingId"));
			}
		} 
		catch (SQLException e) {
			e.printStackTrace();
		}

		refreshMeetingRequestUI();
	}
	
	
	
	
	/**
	 * Creates MeetingRequest UI-components 
	 * and fills it with data from parameter, then
	 * inserts it into given meetingBox
	 * @param meetingBox
	 * @param date
	 * @param description
	 * @param meetingLeader
	 * @param room
	 * @param meetingid
	 */
	private static void createNewMeetingRequestBox(Box meetingBox, String date, 
			String description, String meetingLeader, String room, int meetingId){

		final int finalMeetingId = meetingId;
		Box horizontalBox = Box.createHorizontalBox();
		horizontalBox.setBorder(new LineBorder(Color.LIGHT_GRAY, 1, true));
		horizontalBox.setBounds(203, 0, 208, 17);
		meetingBox.add(horizontalBox);

		Component rigidArea_4 = Box.createRigidArea(new Dimension(10, 20));
		horizontalBox.add(rigidArea_4);
		JLabel lblDate = new JLabel("Date:");
		lblDate.setHorizontalAlignment(SwingConstants.LEFT);
		lblDate.setFont(new Font("Tahoma", Font.BOLD, 11));
		horizontalBox.add(lblDate);
		Component rigidArea_5 = Box.createRigidArea(new Dimension(5, 20));
		horizontalBox.add(rigidArea_5);
		JLabel labelDateText = new JLabel(date);
		horizontalBox.add(labelDateText);

		Component rigidArea_2 = Box.createRigidArea(new Dimension(10, 20));
		horizontalBox.add(rigidArea_2);
		JLabel lblDescription = new JLabel("Description:");
		lblDescription.setHorizontalAlignment(SwingConstants.LEFT);
		lblDescription.setFont(new Font("Tahoma", Font.BOLD, 11));
		horizontalBox.add(lblDescription);
		Component rigidArea_3 = Box.createRigidArea(new Dimension(5, 20));
		horizontalBox.add(rigidArea_3);
		JLabel lblDescriptionText = new JLabel(description);
		horizontalBox.add(lblDescriptionText);

		Component rigidArea_1 = Box.createRigidArea(new Dimension(10, 20));
		horizontalBox.add(rigidArea_1);
		JLabel lblMeetingLeader = new JLabel("Meeting Leader:");
		lblMeetingLeader.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblMeetingLeader.setHorizontalAlignment(SwingConstants.LEFT);
		horizontalBox.add(lblMeetingLeader);
		Component rigidArea = Box.createRigidArea(new Dimension(5, 20));
		horizontalBox.add(rigidArea);
		JLabel lblMeetingLeaderText = new JLabel(meetingLeader);
		horizontalBox.add(lblMeetingLeaderText);

		Component rigidArea_6 = Box.createRigidArea(new Dimension(10, 20));
		horizontalBox.add(rigidArea_6);
		JLabel lblRoom = new JLabel("Room:");
		lblRoom.setHorizontalAlignment(SwingConstants.LEFT);
		lblRoom.setFont(new Font("Tahoma", Font.BOLD, 11));
		horizontalBox.add(lblRoom);
		Component rigidArea_7 = Box.createRigidArea(new Dimension(5, 20));
		horizontalBox.add(rigidArea_7);
		JLabel lblRoomText = new JLabel(room);
		horizontalBox.add(lblRoomText);
		Component verticalStrut = Box.createVerticalStrut(20);
		horizontalBox.add(verticalStrut);

		JButton btnAccept = new JButton("");
		btnAccept.setIcon(new ImageIcon(MainFrame.class.getResource("/images/verify.png")));
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setMeetingStatus(finalMeetingId,"ACCEPTED");
				getMeetingRequest(verticalBoxBottom);
			}
		});
		btnAccept.setBounds(0, 0, 67, 23);
		horizontalBox.add(btnAccept);
		
		JButton btnDecline = new JButton("");
		btnDecline.setIcon(new ImageIcon(MainFrame.class.getResource("/images/deny.png")));
		btnDecline.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setMeetingStatus(finalMeetingId,"DECLINED");
				getMeetingRequest(verticalBoxBottom);
			}
		});
		btnDecline.setBounds(0, 0, 89, 23);
		horizontalBox.add(btnDecline);
	}
	
	
	/**
	 * Refreshes Calendar UI to represent actual updated data
	 */
	private static void refreshCalendarUI(){
		verticalBoxDay1.setVisible(false);
		verticalBoxDay1.setVisible(true);
		verticalBoxDay2.setVisible(false);
		verticalBoxDay2.setVisible(true);
		verticalBoxDay3.setVisible(false);
		verticalBoxDay3.setVisible(true);
	}
	
	/**
	 * Refreshes Calendar UI to represent actual updated data
	 */
	private static void refreshMeetingRequestUI(){
		verticalBoxBottom.setVisible(false);
		verticalBoxBottom.setVisible(true);
	}
	
	
	/**
	 * Formats a 4 char time String to include separator (00:00)
	 * @param timeToConvert
	 * @return
	 */
	private static String formatTime(String timeToConvert){
		return new StringBuffer(timeToConvert).insert(2, ":").toString();
	}
	
	/**
	 * Updates all CalendarBoxes with data from selected date in datePicker
	 */
	private void updateSingleBoxes(){
		getNewCalendar(datePicker.getDate(),verticalBoxDay1, currentUserId, true);
		calendar.setTime(datePicker.getDate());
		labelDay1.setText(dateFormat.format(calendar.getTime()));
		
		calendar.add(Calendar.DATE,1); //increment by one day
		labelDay2.setText(dateFormat.format(calendar.getTime()));
		getNewCalendar(calendar.getTime(),verticalBoxDay2, currentUserId, true);
		
		calendar.add(Calendar.DATE,1); //increment by one day
		labelDay3.setText(dateFormat.format(calendar.getTime()));
		getNewCalendar(calendar.getTime(),verticalBoxDay3, currentUserId, true);
	}
	
	private void updateMultiBoxes(){
		int idForPane2 = reverseEmployeeMap.get(day2comboBox.getSelectedItem());
		int idForPane3 = reverseEmployeeMap.get(day3comboBox.getSelectedItem());
		
		getNewCalendar(datePicker.getDate(),verticalBoxDay1, currentUserId, true);
		getNewCalendar(datePicker.getDate(),verticalBoxDay2,idForPane2, false);
		getNewCalendar(datePicker.getDate(),verticalBoxDay3,idForPane3, false);
		
		labelDay1.setText(dateFormat.format(datePicker.getDate()));
		labelDay2.setText(dateFormat.format(datePicker.getDate()));
		labelDay3.setText(dateFormat.format(datePicker.getDate()));
	}
	

	
	/**
	 * Takes in leaderId from parameter, returns a String with leader's Name
	 * @param leaderId
	 * @return
	 * @throws SQLException
	 */
	private static String getMeetingLeader(int leaderId) throws SQLException{
		
		ResultSet meetingLeaderRes = Main.dbConn.selectFromDatabase(
				"SELECT employee.* FROM employee,meetingRequest WHERE "+
				"employee.employeeId =" + leaderId
				);
		meetingLeaderRes.next();
		return meetingLeaderRes.getString("name");
	}
	
	
	/**
	 * Takes in meetingId from parameter, updates DB
	 * with status set to statusString from parameter.
	 * @param meetingId
	 * @return
	 * @throws SQLException
	 */
	private static void setMeetingStatus(int meetingId, String status) {
		
		try {
			Main.dbConn.modifyDatabase(
					"UPDATE meetingRequest SET status = '"+ status +"' WHERE recipientId = "
					+ currentUserId +
					" AND meetingId = "+ meetingId );
			} 
		catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private void populateComboBox(){
		
		try {
			ResultSet comboBoxRes = Main.dbConn.selectFromDatabase("SELECT * FROM employee");
			day2comboBox.removeAllItems();
			day3comboBox.removeAllItems();
			reverseEmployeeMap = new HashMap<String, Integer>();
			while(comboBoxRes.next()) {
				if (comboBoxRes.getInt("employeeId") != currentUserId){
				reverseEmployeeMap.put(comboBoxRes.getString("name"), comboBoxRes.getInt("employeeId"));
					String employeeName= comboBoxRes.getString("name");
					day2comboBox.addItem(employeeName);
					day3comboBox.addItem(employeeName);
				}
			}	
			day2comboBox.setSelectedItem(currentUserId);
			day3comboBox.setSelectedItem(currentUserId);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	private void showAddMeetingRoom() {
		AddMeetingRoom meetingRoom = new AddMeetingRoom();
		meetingRoom.setVisible(true);
	}
	
	private void showTrackMeetingRequest() {
		TrackMeetingRequest trackMeeting = new TrackMeetingRequest();
		trackMeeting.setVisible(true);
	}
	
	protected void showManageAppointment() {
		try {
			ManageAppointment manageAppointment = new ManageAppointment();
			manageAppointment.setVisible(true);
			} 
		catch (PropertyVetoException e) {
			e.printStackTrace();
			}
		}
}
